define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    grant_url: 'user/user/grant',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'team_id', title: __('Team_id'), visible:false},
                        //{field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        //{field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        //{field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        //{field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        /*{field: 'is_team', title: __('Is_team'), formatter: Table.api.formatter.normal, searchList: {'0': __('Is_team 0'), '1': __('Is_team 1')}},
                        {field: 'is_staff', title: __('Is_staff'), formatter: Table.api.formatter.normal, searchList: {'0': __('Is_staff 0'), '1': __('Is_staff 1')}},
                        {field: 'is_region', title: __('Is_region'), formatter: Table.api.formatter.normal, searchList: {'0': __('Is_region 0'), '1': __('Is_region 1')}},
                        */
                        {field: 'level.name', title: __('Level.name'), operate: 'LIKE', sortable: true},
                        {field: 'identity', title: __('Identity'), operate: false,formatter: Table.api.formatter.normal,searchList: {0: __('Identity 0'), 1: __('Identity 1'), 2: __('Identity 2'), 3: __('Identity 3'),4: __('Identity 4')}},
                       /* {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},*/
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'code', title: __('二维码'),operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        //{field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'setTeam',
                                    title: '指定为团长',
                                    text: '指定为团长',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定指定为团长？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if ((row.identity == 1 || row.is_staff==1 || row.identity==2 || row.identity==3  ||row.team_id>0 )) {
                                            return false;
                                        }else{
                                            return true;
                                        }

                                    },
                                    url: function (row, column) {
                                        var url = 'user/user/setTeam?id=' + row.id + '&identity=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'setTeam',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "取消团长身份",
                                    icon: "",
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.identity != 1) {
                                            return false ;
                                        }
                                        return true;

                                    }
                                },
                                /*{
                                    name: 'setRegion',
                                    title: __('设置区域合伙人'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '设置区域合伙人',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'user/user/setRegion?id=' + row.id;
                                        return url;
                                    },visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.is_team == 1 || row.is_staff == 1 ) {
                                            return false ;
                                        }
                                        return true;

                                    }
                                },*/
                                {
                                    name: 'disable',
                                    title: '禁用',
                                    text: '禁用',
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定禁用账号？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status != 1) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'user/user/interdict?id=' + row.id + '&status=hidden';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                            ]
                            , events: {
                                'click .btn-refund': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请输入备注",
                                        formType: 2,
                                        area: ['200px', '100px'], //自定义文本域宽高
                                        value: row.remark,
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "user/user/setTeam?id=" + row.id + '&identity=0',
                                            data: {remark: value},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                        });
                                    });
                                    return false;
                                },
                                'click .btn-delone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    Layer.confirm(
                                        __('Are you sure you want to delete this item?'),
                                        {
                                            icon: 3,
                                            title: __('Warning'),
                                            offset: [top, left],
                                            shadeClose: true,
                                            btn: [__('OK'), __('Cancel')]
                                        },
                                        function (index) {
                                            var table = $(that).closest('table');
                                            var options = table.bootstrapTable('getOptions');
                                            Table.api.multi("del", row[options.pk], table, that);
                                            Layer.close(index);
                                        }
                                    );
                                },
                                'click .btn-editone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.edit_url;
                                    Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
                                },
                            }, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 获取选中项
            $(document).on("click", ".btn-grant", function () {
                var ids=Table.api.selectedids(table);
                if(ids.length>0){
                    ids=ids.join(",");
                    Backend.api.open('user/grant/grant?ids='+ids, __('发放现金补贴'));
                }else{
                    Backend.api.open('user/grant/grant', __('发放现金补贴'));
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        grant: function () {
            Controller.api.bindevent();
            Form.api.bindevent($("form[role=form]"), function () {}, function () {}, function (success, error) {
                var money=$("#c-money").val();
                var day=$("#c-day").val();
                    var str="<p>发放金额："+money+"(元)</p>"
                        +"<p>发放天数："+day+"(天)</p>"+"<p>每日发放："+money/day+"(元)</p>"
                        +"<p>是否确认发放？</p>";
                layer.confirm(str, {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    Form.api.submit($("form[role=form]"), function (data, ret) {
                        //如果我们需要在提交表单成功后做跳转，可以在此使用location.href="链接";进行跳转
                        setTimeout(function () {
                            parent.Layer.close(parent.Layer.getFrameIndex(window.name));
                        }, 1000);
                    });
                    Layer.close(index);
                }, function () {
                });
                return false;

            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        below: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/below',
                  /*  add_url: 'user/user/add',
                    edit_url: 'user/user/edit',*/
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });
            
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'team_id', title: __('Team_id'), visible:false},
                        //{field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        //{field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        //{field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        //{field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        /*{field: 'is_team', title: __('Is_team'), formatter: Table.api.formatter.normal, searchList: {'0': __('Is_team 0'), '1': __('Is_team 1')}},
                        {field: 'is_staff', title: __('Is_staff'), formatter: Table.api.formatter.normal, searchList: {'0': __('Is_staff 0'), '1': __('Is_staff 1')}},
                        {field: 'is_region', title: __('Is_region'), formatter: Table.api.formatter.normal, searchList: {'0': __('Is_region 0'), '1': __('Is_region 1')}},
                        */
                        {field: 'level.name', title: __('Level.name'), operate: 'LIKE', sortable: true},
                        {field: 'identity', title: __('Identity'), operate: false,formatter: Table.api.formatter.normal,searchList: {0: __('Identity 0'), 1: __('Identity 1'), 2: __('Identity 2'), 3: __('Identity 3'),4: __('Identity 4')}},
                       /* {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},*/
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        /*{field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},

                        {field: 'staff_status', title: __('Staff_status'), formatter: Table.api.formatter.status, searchList: {0: __('Staff_status 0'), 1: __('Staff_status 1')}},
                        */{field: 'operate', title: __('Operate'), table: table,
                            /*buttons: [
                                {
                                    name: 'setTeam',
                                    title: '指定为团长',
                                    text: '指定为团长',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定指定为团长？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.identity == 1 || row.is_staff==1 || row.identity==2 || row.identity==3) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'user/user/setTeam?id=' + row.id + '&is_team=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'setTeam',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "取消团长身份",
                                    icon: "",
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.identity != 1) {
                                            return false ;
                                        }
                                        return true;

                                    }
                                },
                                {
                                    name: 'setRegion',
                                    title: __('设置区域合伙人'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '设置区域合伙人',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'user/user/setRegion?id=' + row.id;
                                        return url;
                                    },visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.is_team == 1 || row.is_staff == 1 ) {
                                            return false ;
                                        }
                                        return true;

                                    }
                                },
                                {
                                    name: 'disable',
                                    title: '禁用',
                                    text: '禁用',
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定禁用账号？',
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status != 1) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'user/user/interdict?id=' + row.id + '&status=hidden';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                            ]
                            ,*/ events: {
                                'click .btn-refund': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请输入备注",
                                        formType: 2,
                                        area: ['200px', '100px'], //自定义文本域宽高
                                        value: row.remark,
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "user/user/setTeam?id=" + row.id + '&identity=0',
                                            data: {remark: value},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                        });
                                    });
                                    return false;
                                },
                                'click .btn-delone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    Layer.confirm(
                                        __('Are you sure you want to delete this item?'),
                                        {
                                            icon: 3,
                                            title: __('Warning'),
                                            offset: [top, left],
                                            shadeClose: true,
                                            btn: [__('OK'), __('Cancel')]
                                        },
                                        function (index) {
                                            var table = $(that).closest('table');
                                            var options = table.bootstrapTable('getOptions');
                                            Table.api.multi("del", row[options.pk], table, that);
                                            Layer.close(index);
                                        }
                                    );
                                },
                                'click .btn-editone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.edit_url;
                                    Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
                                },
                            }, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});